package com.minotauro.i18n.base;

import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.SystemUtils;

import com.minotauro.i18n.scanner.Visitor;

public class I18NVisitor implements Visitor {

  private I18NIsProcessable isProcessable;
  private BaseI18NMain app;
  private Set<String> processedPrefixes = new HashSet<String>();
  private File output;

  // --------------------------------------------------------------------------------

  public I18NVisitor(I18NIsProcessable isProcessable, File output) {
    this.isProcessable = isProcessable;
    this.output = output;
  }

  // --------------------------------------------------------------------------------

  @Override
  public void visit(File file, List<String> path) {

    String fileNamePrefix = isProcessable.groupPattern.get(1);
    String fileName = fileNamePrefix + isProcessable.groupPattern.get(2);
    String pathName = pathListToPathName(path);
    String inputDirectory = getRootPath(file, pathName)
        + SystemUtils.FILE_SEPARATOR;
    String outputDirectory = output.getPath() + SystemUtils.FILE_SEPARATOR;
    String pckName = pathListToPackName(path);

    app = new BaseI18NMain();

    String _fileName = isProcessable.groupPattern.get(0);
    String _pathName = pathListToPathName(path);

    if (processedPrefixes.contains(_pathName + _fileName)) {
      return;
    }

    processedPrefixes.add(_pathName + _fileName);

    if (fileNamePrefix.equals("I18N")) {
      try {
        System.out.println("está generando i18n");
        app.i18n(fileName, inputDirectory, outputDirectory, pckName);

      } catch (Exception e) {
        e.printStackTrace();
      }
    } else if (fileNamePrefix.equals("Date")) {
      try {
        app.date(fileName, inputDirectory, outputDirectory, pckName);
      } catch (Exception e) {
        e.printStackTrace();
      }
    } else if (fileNamePrefix.equals("Numb")) {
      try {
        app.numb(fileName, inputDirectory, outputDirectory, pckName);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

  // --------------------------------------------------------------------------------

  public String pathListToPathName(List<String> pathList) {
    String path = "";

    for (int i = 0; i < pathList.size(); i++) {
      path = path + pathList.get(i);
      path = path + SystemUtils.FILE_SEPARATOR;

      //      if (i < pathList.size() - 1) {
      //        path = path + SystemUtils.FILE_SEPARATOR;
      //      }
    }
    return path;
  }

  // --------------------------------------------------------------------------------

  public String pathListToPackName(List<String> pathList) {
    String pack = "";

    for (int i = 0; i < pathList.size(); i++) {
      pack = pack + pathList.get(i);

      if (i < pathList.size() - 1) {
        pack = pack + ".";
      }
    }
    return pack;
  }

  // --------------------------------------------------------------------------------

  public String getRootPath(File file, String path) {
    String rootPath = "";
    rootPath = (file.getAbsolutePath()).replace(file.getName(), "");
    rootPath = rootPath.replace(path, "");
    rootPath = rootPath.replace(SystemUtils.USER_DIR, "");
    return rootPath;
  }

  // --------------------------------------------------------------------------------

}